Passed
Push — master ( 33ca92...23b088 )
by Ahmad
10:28
created

room.js ➔ updateCurrentSettings   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
eloc 7
1
// BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
2
//
3
// Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
4
//
5
// This program is free software; you can redistribute it and/or modify it under the
6
// terms of the GNU Lesser General Public License as published by the Free Software
7
// Foundation; either version 3.0 of the License, or (at your option) any later
8
// version.
9
//
10
// BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public License along
15
// with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
16
17
// Room specific js for copy button and email link.
18
$(document).on('turbolinks:load', function(){
19
  var controller = $("body").data('controller');
20
  var action = $("body").data('action');
21
22
  // highlight current room
23
  $('.room-block').removeClass('current');
24
  $('a[href="' + window.location.pathname + '"] .room-block').addClass('current');
25
26
  // Only run on room pages.
27
  if (controller == "rooms" && action == "show"){
28
    // Display and update all fields related to creating a room in the createRoomModal
29
    $("#create-room-block").click(function(){
30
      showCreateRoom(this)
31
    })
32
33
    checkIfAutoJoin()
34
  }
35
36
    // Autofocus on the Room Name label when creating a room only
37
  $('#createRoomModal').on('shown.bs.modal', function (){
38
    if ($(".create-only").css("display") == "block"){
39
      $('#create-room-name').focus()
40
    }
41
  })
42
43
  if (controller == "rooms" && action == "show" || controller == "admins" && action == "server_rooms"){
44
    // Display and update all fields related to creating a room in the createRoomModal
45
    $(".update-room").click(function(){
46
      showUpdateRoom(this)
47
    })
48
49
    $(".delete-room").click(function() {
50
      showDeleteRoom(this)
51
    })
52
53
    $('.selectpicker').selectpicker({
54
      liveSearchPlaceholder: getLocalizedString('javascript.search.start')
55
    });
56
    // Fixes turbolinks issue with bootstrap select
57
    $(window).trigger('load.bs.select.data-api');
58
59
    $(".share-room").click(function() {
60
      // Update the path of save button
61
      $("#save-access").attr("data-path", $(this).data("path"))
62
63
      // Get list of users shared with and display them
64
      displaySharedUsers($(this).data("users-path"))
65
    })
66
67
    $("#shareRoomModal").on("show.bs.modal", function() {
68
      $(".selectpicker").selectpicker('val','')
69
    })
70
71
    $(".bootstrap-select").on("click", function() {
72
      $(".bs-searchbox").siblings().hide()
73
    })
74
75
    $(".bs-searchbox input").on("input", function() {
76
      if ($(".bs-searchbox input").val() == '' || $(".bs-searchbox input").val().length < 3) {
77
        $(".bs-searchbox").siblings().hide()
78
      } else {
79
        $(".bs-searchbox").siblings().show()
80
      }
81
    })
82
83
    $(".remove-share-room").click(function() {
84
      $("#remove-shared-confirm").parent().attr("action", $(this).data("path"))
85
    })
86
87
    // User selects an option from the Room Access dropdown
88
    $(".bootstrap-select").on("changed.bs.select", function(){
89
      // Get the uid of the selected user
90
      let uid = $(".selectpicker").selectpicker('val')
91
92
      // If the value was changed to blank, ignore it
93
      if (uid == "") return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
94
95
      let currentListItems = $("#user-list li").toArray().map(user => $(user).data("uid"))
96
97
      // Check to make sure that the user is not already there
98
      if (!currentListItems.includes(uid)) {
99
        // Create the faded list item and display it
100
        let option = $("option[value='" + uid + "']")
101
102
        let listItem = document.createElement("li")
103
        listItem.setAttribute('class', 'list-group-item text-left not-saved add-access');
104
        listItem.setAttribute("data-uid", uid)
105
106
        let spanItemAvatar = document.createElement("span"),
107
            spanItemName = document.createElement("span"),
108
            spanItemUser = document.createElement("span");
109
        spanItemAvatar.setAttribute('class', 'avatar float-left mr-2');
110
        spanItemAvatar.innerText = option.text().charAt(0);
111
        spanItemName.setAttribute('class', 'shared-user');
112
        spanItemName.innerText = option.text();
113
        spanItemUser.setAttribute('class', 'text-muted');
114
        spanItemUser.innerText = option.data('subtext');
115
        spanItemName.append(spanItemUser);
116
117
        listItem.innerHTML = "<span class='text-primary float-right shared-user cursor-pointer' onclick='removeSharedUser(this)'><i class='fas fa-times'></i></span>"
118
        listItem.prepend(spanItemName);
119
        listItem.prepend(spanItemAvatar);
120
121
        $("#user-list").append(listItem)
122
      }
123
    })
124
125
    $("#presentation-upload").change(function(data) {
126
      var file = data.target.files[0]
127
128
      // Check file type and size to make sure they aren't over the limit
129
      if (validFileUpload(file)) {
130
        $("#presentation-upload-label").text(file.name)
131
      } else {
132
        $("#invalid-file-type").show()
133
        $("#presentation-upload").val("")
134
        $("#presentation-upload-label").text($("#presentation-upload-label").data("placeholder"))
135
      }
136
    })
137
138
    $(".preupload-room").click(function() {
139
      updatePreuploadPresentationModal(this)
140
    })
141
142
    $("#remove-presentation").click(function(data) {
143
      removePreuploadPresentation($(this).data("remove"))
144
    })
145
  }
146
});
147
148
function copyInvite() {
149
  $('#invite-url').select()
150
  if (document.execCommand("copy")) {
151
    $('#invite-url').blur();
152
    copy = $("#copy-invite")
0 ignored issues
show
Bug introduced by
The variable copy seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.copy.
Loading history...
153
    copy.addClass('btn-success');
154
    copy.html("<i class='fas fa-check mr-1'></i>" + getLocalizedString("copied"))
155
    setTimeout(function(){
156
      copy.removeClass('btn-success');
157
      copy.html("<i class='fas fa-copy mr-1'></i>" + getLocalizedString("copy"))
158
    }, 1000)
159
  }
160
}
161
162
function copyAccess() {
163
  $('#copy-code').attr("type", "text")
164
  $('#copy-code').select()
165
  if (document.execCommand("copy")) {
166
    $('#copy-code').attr("type", "hidden")
167
    copy = $("#copy-access")
0 ignored issues
show
Bug introduced by
The variable copy seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.copy.
Loading history...
168
    copy.addClass('btn-success');
169
    copy.html("<i class='fas fa-check mr-1'></i>" + getLocalizedString("copied"))
170
    setTimeout(function(){
171
      copy.removeClass('btn-success');
172
      copy.html("<i class='fas fa-copy mr-1'></i>" + getLocalizedString("room.copy_access"))
173
    }, 1000)
174
  }
175
}
176
177
function showCreateRoom(target) {
0 ignored issues
show
Unused Code introduced by
The parameter target is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
178
  $("#create-room-name").val("")
179
  $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
180
  $("#room_access_code").val(null)
181
182
  $("#createRoomModal form").attr("action", $("body").data('relative-root'))
183
  $("#room_mute_on_join").prop("checked", $("#room_mute_on_join").data("default"))
184
  $("#room_require_moderator_approval").prop("checked", $("#room_require_moderator_approval").data("default"))
185
  $("#room_anyone_can_start").prop("checked", $("#room_anyone_can_start").data("default"))
186
  $("#room_all_join_moderator").prop("checked", $("#room_all_join_moderator").data("default"))
187
  $("#room_recording").prop("checked", $("#room_recording").data("default"))
188
189
  //show all elements & their children with a create-only class
190
  $(".create-only").each(function() {
191
    $(this).show()
192
    if($(this).children().length > 0) { $(this).children().show() }
193
  })
194
195
  //hide all elements & their children with a update-only class
196
  $(".update-only").each(function() {
197
    $(this).attr('style',"display:none !important")
198
    if($(this).children().length > 0) { $(this).children().attr('style',"display:none !important") }
199
  })
200
}
201
202
function showUpdateRoom(target) {
203
  var modal = $(target)
204
  var update_path = modal.closest(".room-block").data("path")
205
  var settings_path = modal.data("settings-path")
206
  $("#create-room-name").val(modal.closest(".room-block").find(".room-name-text").text().trim())
207
  $("#createRoomModal form").attr("action", update_path)
208
209
  //show all elements & their children with a update-only class
210
  $(".update-only").each(function() {
211
    $(this).show()
212
    if($(this).children().length > 0) { $(this).children().show() }
213
  })
214
215
  //hide all elements & their children with a create-only class
216
  $(".create-only").each(function() {
217
    $(this).attr('style',"display:none !important")
218
    if($(this).children().length > 0) { $(this).children().attr('style',"display:none !important") }
219
  })
220
221
  updateCurrentSettings(settings_path)
222
223
  var accessCode = modal.closest(".room-block").data("room-access-code")
224
225
  if(accessCode){
226
    $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code") + ": " + accessCode)
227
    $("#room_access_code").val(accessCode)
228
  } else {
229
    $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
230
    $("#room_access_code").val(null)
231
  }
232
}
233
234
function showDeleteRoom(target) {
235
  $("#delete-header").text(getLocalizedString("modal.delete_room.confirm").replace("%{room}", $(target).data("name")))
236
  $("#delete-confirm").parent().attr("action", $(target).data("path"))
237
}
238
239
//Update the createRoomModal to show the correct current settings
240
function updateCurrentSettings(settings_path){
241
  // Get current room settings and set checkbox
242
  $.get(settings_path, function(settings) {
243
    $("#room_mute_on_join").prop("checked", $("#room_mute_on_join").data("default") || settings.muteOnStart)
244
    $("#room_require_moderator_approval").prop("checked", $("#room_require_moderator_approval").data("default") || settings.requireModeratorApproval)
245
    $("#room_anyone_can_start").prop("checked", $("#room_anyone_can_start").data("default") || settings.anyoneCanStart)
246
    $("#room_all_join_moderator").prop("checked", $("#room_all_join_moderator").data("default") || settings.joinModerator)
247
    $("#room_recording").prop("checked", $("#room_recording").data("default") || Boolean(settings.recording))
248
  })
249
}
250
251
function generateAccessCode(){
252
  const accessCodeLength = 6
253
  var validCharacters = "0123456789"
254
  var accessCode = ""
255
256
  for( var i = 0; i < accessCodeLength; i++){
257
    accessCode += validCharacters.charAt(Math.floor(Math.random() * validCharacters.length));
258
  }
259
260
  $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code") + ": " + accessCode)
261
  $("#room_access_code").val(accessCode)
262
}
263
264
function ResetAccessCode(){
265
  $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
266
  $("#room_access_code").val(null)
267
}
268
269
function saveAccessChanges() {
270
  let listItemsToAdd = $("#user-list li:not(.remove-shared)").toArray().map(user => $(user).data("uid"))
271
272
  $.post($("#save-access").data("path"), {add: listItemsToAdd})
273
}
274
275
// Get list of users shared with and display them
276
function displaySharedUsers(path) {
277
  $.get(path, function(users) {
278
    // Create list element and add to user list
279
    var user_list_html = ""
280
281
    users.forEach(function(user) {
282
      user_list_html += "<li class='list-group-item text-left' data-uid='" + user.uid + "'>"
283
284
      if (user.image) {
285
        user_list_html += "<img id='user-image' class='avatar float-left mr-2' src='" + user.image + "'></img>"
286
      } else {
287
        user_list_html += "<span class='avatar float-left mr-2'>" + user.name.charAt(0) + "</span>"
288
      }
289
      user_list_html += "<span class='shared-user'>" + user.name + "<span class='text-muted ml-1'>" + user.uid + "</span></span>"
290
      user_list_html += "<span class='text-primary float-right shared-user cursor-pointer' onclick='removeSharedUser(this)'><i class='fas fa-times'></i></span>"
291
      user_list_html += "</li>"
292
    })
293
294
    $("#user-list").html(user_list_html)
295
  });
296
}
297
298
// Removes the user from the list of shared users
299
function removeSharedUser(target) {
300
  let parentLI = target.closest("li")
301
302
  if (parentLI.classList.contains("not-saved")) {
303
    parentLI.parentNode.removeChild(parentLI)
304
  } else {
305
    parentLI.removeChild(target)
306
    parentLI.classList.add("remove-shared")
307
  }
308
}
309
310
function updatePreuploadPresentationModal(target) {
311
  $.get($(target).data("settings-path"), function(presentation) {
312
    if(presentation.attached) {
313
      $("#current-presentation").show()
314
      $("#presentation-name").text(presentation.name)
315
      $("#change-pres").show()
316
      $("#use-pres").hide()
317
    } else {
318
      $("#current-presentation").hide()
319
      $("#change-pres").hide()
320
      $("#use-pres").show()
321
    }
322
  });
323
  
324
  $("#preuploadPresentationModal form").attr("action", $(target).data("path"))
325
  $("#remove-presentation").data("remove",  $(target).data("remove"))
326
  
327
  // Reset values to original to prevent confusion
328
  $("#presentation-upload").val("")
329
  $("#presentation-upload-label").text($("#presentation-upload-label").data("placeholder"))
330
  $("#invalid-file-type").hide()
331
}
332
333
function removePreuploadPresentation(path) {
334
  $.post(path, {})
335
}
336
337
function validFileUpload(file) {
338
  return file.size/1024/1024 <= 30
339
}
340
341
// Automatically click the join button if this is an action cable reload
342
function checkIfAutoJoin() {
343
  var url = new URL(window.location.href)
0 ignored issues
show
Bug introduced by
The variable URL seems to be never declared. If this is a global, consider adding a /** global: URL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
344
345
  if (url.searchParams.get("reload") == "true") {
346
    $("#joiner-consent").click()
347
    $("#room-join").click()
348
  }
349
}
350